
package com.sasis.dao.ibatis;

import java.util.List;

import com.sasis.dao.ibatis.BaseDaoiBATIS;
import com.sasis.model.City;
import com.sasis.dao.CityDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class CityDaoiBatis extends BaseDaoiBATIS implements CityDao {

    /**
     * @see com.sasis.dao.CityDao#getCitys(com.sasis.model.City)
     */
    public List getCitys(final City city) {
          return getSqlMapClientTemplate().queryForList("getCitys", city);
    }

    /**
     * @see com.sasis.dao.CityDao#getCitys(com.sasis.model.City)
     */
    public List selectCitys(final City city) {
          return getSqlMapClientTemplate().queryForList("selectCitys", city);
    }

    /**
     * @see com.sasis.dao.CityDao#getCity(Integer id)
     */
    public City getCity(Integer id) {
        City city = (City) getSqlMapClientTemplate().queryForObject("getCity", id);

        if (city == null) {
            throw new ObjectRetrievalFailureException(City.class, id);
        }

        return city;
    }

    /**
     * @see com.sasis.dao.CityDao#saveCity(City city)
     */    
    public void saveCity(final City city) {
        Integer id = city.getId();
        // check for new record
        if (id == null) {
            id = (Integer) getSqlMapClientTemplate().insert("addCity", city);
        } else {
            getSqlMapClientTemplate().update("updateCity", city);
        }
        if( id == null ) {
            throw new ObjectRetrievalFailureException(City.class, id);
        }
    }

    /**
     * @see com.sasis.dao.CityDao#removeCity(Integer id)
     */
    public void removeCity(City city) {
        getSqlMapClientTemplate().update("deleteCity", city);
    }
}
